package com.lagou.factory;

import com.lagou.utils.TransactionManager;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 代理对象工厂：用于生成代理对象
 *
 * @author: liufh
 * @create: 2021-07-11 17:12
 **/
public class ProxyFactory {

    private TransactionManager transactionManager;

    public void setTransactionManager(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    //将类创建交给容器管理，需将private构造器去掉
//    private ProxyFactory(){
//    }
//
//    private static final ProxyFactory proxyFactory = new ProxyFactory();
//
//    public static ProxyFactory getInstance() {
//        return proxyFactory;
//    }

    /**
     * Jdk动态代理
     * @param obj  委托对象
     * @return   代理对象
     */
    public Object getJdkProxy(Object obj) {
        //获取代理对象
        Object proxyObject = Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object result = null;
                try {
                    //开启事务(关闭事务的自动提交)
                    System.out.println("开启事务");
                    transactionManager.beginTransaction();
                    //业务方法调用
                    result = method.invoke(obj, args);
                    //提交事务
                    System.out.println("提交事务");
                    transactionManager.commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    //回滚事务
                    System.out.println("回滚事务");
                    transactionManager.rollback();
                    //抛出异常便于上层servlet捕获
                    throw e;
                }
                return result;
            }
        });
        return proxyObject;
    }


    /**
     * 使用cglib动态代理生成代理对象
     * @param obj 委托对象
     * @return
     */
    public Object getCglibProxy(Object obj) {
        Object proxyObject = Enhancer.create(obj.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                Object result = null;
                try {
                    //开启事务(关闭事务的自动提交)
                    transactionManager.beginTransaction();
                    //业务方法调用
                    result = method.invoke(obj, objects);
                    //提交事务
                    transactionManager.commit();
                } catch (Exception e) {
                    e.printStackTrace();
                    //回滚事务
                    transactionManager.rollback();
                    //抛出异常便于上层servlet捕获
                    throw e;
                }
                return result;
            }
        });
        return proxyObject;
    }

}
